<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        button {
            width: 100px;
            height: 50px;
            cursor: pointer;
            margin: 20px;
        }
        .color{
            background-color: chartreuse;
        }
    </style>
</head>

<body>
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
    <button>5</button>
    <button>6</button>
    <script>
        var btnArr = document.querySelectorAll("button");
        for (var i = 0; i < btnArr.length; i++) {
            //为每一个按钮绑定事件，用到for循环遍历
            // onmouseover==指针移动到图像上执行
            btnArr[i].onmouseover = function () {
                for (var j = 0; j < btnArr.length; j++) {
                         btnArr[j].className="";
                }
                this.className="color";
            }

        }
        // 鼠标离开时还原图像背景颜色
        for(var i=0;i<btnArr.length;i++){
            //onmouseout 将鼠标指针移出图像时执行 JavaScript：
            btnArr.onmouseout=function(){
                this.className="";
            }
        }

    </script>
</body>

</html>